package com.example.classload;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        User user=null;
        for (int i = 0; i < 3; i++) {
            user =new User(i,"10000"+i,"qijiang"+i);
            list.add(user);
        }
        user =new User(1,"10000"+2,"qijiang"+3);
        list.add(user);
//        List<User> collect = list.stream()
//                .filter(p->p.getUserId() !=null)
//                .filter(distinctByKey(p -> p.getUserId()))
//                .collect(Collectors.toList());
//        Map<Integer, List<User>> collect = list.stream()
//                .filter(p -> p.getUserId() != null)
//               // .filter(distinctByKey(p -> p.getUserId()))
//                //.collect(Collectors.toList());
//                .collect(Collectors.groupingBy(User::getUserId));
//        System.out.println(collect);
//        Optional.ofNullable(list).orElse(s)
//                .collect(Collectors.toList());
        System.out.println(list.stream().map(s->s.getUserId()).collect(Collectors.toList()));
    }


    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)
    {
        Map<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}
